package ar.com.tristeslos3tigres.rssfeedreader;

import android.annotation.SuppressLint;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author sebastian.carrozzo
 * 
 */
public class RssFeedItem {
	private String id;
	private Date published;
	private Date updated;
	private String category;
	private RssFeedTextItem title;
	private RssFeedTextItem description;

	private List<RssFeedLinkItem> links;

	public RssFeedItem() {
		title = new RssFeedTextItem();
		description = new RssFeedTextItem();
		links = new ArrayList<RssFeedLinkItem>();
	}

	public RssFeedTextItem getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title.setContent(title);
	}

	public List<RssFeedLinkItem> getLinks() {
		return links;
	}

	public void addLinks(RssFeedLinkItem links) {
		this.links.add(links);
	}

	public RssFeedTextItem getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description.setContent(description);
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Date getPublished() {
		return published;
	}

	public void setPublished(Date published) {
		this.published = published;
	}

	@SuppressLint("SimpleDateFormat")
	public void setPublished(String published) {
		SimpleDateFormat formatoDelTexto = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
		Date fecha = null;
		try {
			fecha = formatoDelTexto.parse(published);
		} catch (Exception ex) {
			//ex.printStackTrace();
		}

		this.published = fecha;
	}

	public Date getUpdated() {
		return updated;
	}

	public void setUpdated(Date updated) {
		this.updated = updated;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public class RssFeedTextItem {
		private RssFeedTextItemType type;
		private String content;

		public RssFeedTextItem() {

		}

		public RssFeedTextItem(RssFeedTextItemType type, String content) {
			this(content);
			this.type = type;
		}

		public RssFeedTextItem(String content) {
			this();
			this.content = content;
		}

		public RssFeedTextItemType getType() {
			return type;
		}

		public void setType(RssFeedTextItemType type) {
			this.type = type;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}

	}

	public class RssFeedLinkItem {
		private RssFeedLinkItemType type;
		private RssFeedLinkItemRel rel;
		private String urlString;
		private URL url;

		private RssFeedLinkItem() {

		}

		private RssFeedLinkItem(RssFeedLinkItemType type,
				RssFeedLinkItemRel rel, String urlString, URL url) {
			this();
			this.type = type;
			this.rel = rel;
			this.urlString = urlString;
			this.url = url;
		}

		public RssFeedLinkItem(RssFeedLinkItemType type,
				RssFeedLinkItemRel rel, String urlString)
				throws MalformedURLException {
			this(type, rel, urlString, new URL(urlString));
		}

		public RssFeedLinkItem(RssFeedLinkItemType type,
				RssFeedLinkItemRel rel, URL url) {
			this(type, rel, url.toString(), url);
		}

		public RssFeedLinkItem(String urlString) throws MalformedURLException {
			this(RssFeedLinkItemType.NULL, RssFeedLinkItemRel.NULL, urlString);
		}

		public RssFeedLinkItemType getType() {
			return type;
		}

		public void setType(RssFeedLinkItemType type) {
			this.type = type;
		}

		public RssFeedLinkItemRel getRel() {
			return rel;
		}

		public void setRel(RssFeedLinkItemRel rel) {
			this.rel = rel;
		}

		public String getUrlString() {
			return urlString;
		}

		public void setUrlString(String urlString) {
			this.urlString = urlString;
		}

		public URL getUrl() {
			return url;
		}

		public void setUrl(URL url) {
			this.url = url;
		}

	}

	public enum RssFeedLinkItemType {
		NULL, HTML, TEXT, ATOM_XML
	}

	public enum RssFeedLinkItemRel {
		NULL, REPLIES, EDIT, SELF, ALTERNATE
	}

	public enum RssFeedTextItemType {
		HTML, TEXT;
		@SuppressLint("DefaultLocale")
		public static RssFeedTextItemType Parse(String value){
			if (value.toUpperCase().equals("TEXT"))
				return RssFeedTextItemType.TEXT;
			else if (value.toUpperCase().equals("HTML"))
				return RssFeedTextItemType.HTML;
			else
				return null;
		}
	}
}